﻿using Model.Admin;
using Services.WXProjectAdmin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xproject.Util;
using Services.WeiXinProject;

namespace WeiXinpProAdmin.Controllers
{
    public class LoginController : CommonBaseController
    {
        AdminUserInfoServices m_AdminUserInfoServices;
      //  WeiXinConfigServices m_WeiXinConfigServices;

        public LoginController(AdminUserInfoServices mAdminUserInfoServices
        //,WeiXinConfigServices mWeiXinConfigServices
            )
        {
            m_AdminUserInfoServices = mAdminUserInfoServices;
        //    m_WeiXinConfigServices = mWeiXinConfigServices;
        }
        // GET: Login
        public ActionResult Index()
        {
          
            return View();
        }
        public ActionResult Login(string gotoUrl)
        {
           //var list =  m_WeiXinConfigServices.GetAllWeiXinConfig();
            ViewBag.gotoUrl = gotoUrl;
            return View();
        }

        public JsonResult LogOut()
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = true;
            this.ClearWxShopSession();
            jsonData = new { Success = success, Data = result };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UserLogin(FormCollection collection)
        {
            var jsonData = new Object();
            string userName = collection["userName"];
            string password = collection["password"];
            string md5key = CommonHelper.MD5(password);
            AdminUser user = m_AdminUserInfoServices.GetUserInfoByPwd(userName, md5key);
            bool result = false;
            string data = string.Empty;
            if (user != null)
            {
                this.SetSession(user, user.ID);
                //获取菜单信息
                List<AdminMenu> menus =  m_AdminUserInfoServices.GetUserMenus(user.UserID,Site);
                System.Web.HttpRuntime.Cache.Insert("CACHE_USER_MENU_" + user.ID, menus, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);
               
                string gotoUrl = this.GetDomain + CommonHelper.GetAppSettings("Default_Site_IndexPage");//去用户中心
                result = true;
                data = gotoUrl;
                jsonData = new { Success = result, Data = data };
            }
            else
            {
                result = false;
                data = "登录失败";
            }
            jsonData = new { Success = result, Data = data };
            return Json(jsonData , JsonRequestBehavior.AllowGet);
        }

     
    }
}